package com.ran.java_gobang.serivce;

import com.ran.java_gobang.game.request.UserLoginRequest;
import com.ran.java_gobang.game.request.UserRegisterRequest;
import com.ran.java_gobang.mapper.UserMapper;
import com.ran.java_gobang.model.User;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ran
 * Date: 2025-08-27
 * Time: 21:51
 */
@Service
@Slf4j
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public UserLoginRequest login(UserLoginRequest userLoginRequest, HttpServletRequest request) {
        User user = userMapper.selectByName(userLoginRequest.getUserName());
        // 查不到用户/密码不正确
        if (user == null || !user.getPassword().equals(userLoginRequest.getPassword())) {
            // 登录失败
            log.error("登录失败, 用户名: " + userLoginRequest.getUserName() + " ,输入的密码: " + userLoginRequest.getPassword());
//            return new User(); // 失败返回一个空的对象
            throw new RuntimeException("用户名或密码错误!");
        }
        userLoginRequest.setUserId(user.getUserId());
        userLoginRequest.setFlag(true);
        userLoginRequest.setMessage(user.getUserName()+" 登陆成功!");
        log.info("登陆成功的用户对象,user: {}",user);
        HttpSession session = request.getSession(true);// 没有当前会话那么创建一个空session来设置
        session.setAttribute("user",user);
        return userLoginRequest;
    }

    public UserRegisterRequest register(UserRegisterRequest userRegisterRequest) {
        if (userRegisterRequest == null || userRegisterRequest.getUserName() == null || userRegisterRequest.getPassword() == null) {
            return null;
        }
        User user1 = userMapper.selectByName(userRegisterRequest.getUserName());
            if (user1 != null) {
                throw new RuntimeException("该用户名已注册!");
            }

        User user = new User();
            BeanUtils.copyProperties(userRegisterRequest,user);
            Integer insert = userMapper.insert(user);
            log.info("注册成功的用户, user: {}",user);
            if (insert != 1) {
                throw new RuntimeException("用户注册失败!");
            }
            userRegisterRequest.setFlag(true);
            userRegisterRequest.setMessage("注册成功!");
            return userRegisterRequest;
    }

    public User getUserInfo(HttpServletRequest request) {
        try {
            HttpSession session = request.getSession(false); // 没有这个session时不在创建新的
            User user = (User) session.getAttribute("user");
            // 重新从数据库查数据, 来做到更新数据'
            User newUser = userMapper.selectByName(user.getUserName());
            return newUser;
        }catch (NullPointerException e) {
            log.error("获取session空指针异常, e: ",e);
            return new User();
        }
    }

    public void userWin(int winUserId) {
        userMapper.userWin(winUserId);
    }

    public void userLose(int loseUserId) {
        userMapper.userLose(loseUserId);
    }

    public void userDraw(int userId) {
        userMapper.userDraw(userId);
    }
}
